package edu.gatech.fiveminutesleft.util;

import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import edu.gatech.fiveminutesleft.storage.StorageAddress;

/**
 * A static class with utilities to convert JSON to ToDoList model 
 * representations and vice versa.
 * 
 * @author Nick Johnson
 */
public class JSONWrapper {

	/**
	 * Convert a task representation to a JSON object.
	 * 
	 * @param task Task representation.
	 * @return JSON object representing the input task.
	 */
	public static JSONObject taskToJSON(Map<String, String> task) {
		JSONObject object = new JSONObject();
		
		try {
			for (Object key : task.keySet()) {
				object.put(key.toString(), task.get(key).toString());
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return object;
	}
	
	/**
	 * Convert a task list representation to a JSON array.
	 * 
	 * @param list Task list representation.
	 * @return JSON array representing the input task list.
	 */
	public static JSONArray taskListToJSON(List<StorageAddress> list) {
		JSONArray array = new JSONArray();
		
		for (StorageAddress addr : list) {
			array.put(addr.toString());
		}
		
		return array;
	}
	
}
